﻿using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using YFrameWork.RunTime;


namespace YFrameWork.Editor
{
    public class ChangeFontTable
    {

        [FormerlySerializedAs("ChangeAssetData")]
        [TableList(IsReadOnly = true, AlwaysExpanded = true), ShowInInspector]
        private readonly ChangeFontWrapper mChangeFontWrapper;

        public ChangeFontObject this[int index]
        {
            get { return this.mChangeFontWrapper.mChangeAssetData; }

        }

        public ChangeFontTable(ChangeFontObject characters)
        {
            this.mChangeFontWrapper = new ChangeFontWrapper(characters);
        }


        public static void ChangeTextFont(Transform trans, Font font)
        {
            var text = trans.GetComponent<Text>();
            if (text != null)
            {
                ChangeTextFont(text, font);
            }
        }
        /// <summary>
        /// 替换一个text的字体
        /// </summary>
        /// <param name="text"></param>
        /// <param name="font"></param>
        public static void ChangeTextFont(Text text, Font font)
        {
            text.font = font;
            RectTransform rect = text.rectTransform;
            float maxHeight = font.lineHeight * text.fontSize / (float)font.fontSize + 5;
            maxHeight = Mathf.Max(rect.sizeDelta.y, maxHeight);
            rect.sizeDelta = new Vector2(rect.sizeDelta.x, maxHeight);
        }

        /// <summary>
        /// 获取一个物体下所有的transfrom组件
        /// </summary>
        /// <param name="tran"></param>
        /// <returns></returns>
        public static List<Transform> GetChildTransfrom(Transform tran)
        {
            List<Transform> Items = new List<Transform>();
            for (int i = 0; i < tran.childCount; i++)
            {
                Transform ChildTran = tran.GetChild(i);
                foreach (var item in GetChildTransfrom(ChildTran))
                    Items.Add(item);
                Items.Add(ChildTran);
            }
            return Items;
        }


        private class ChangeFontWrapper
        {
            private ChangeFontObject changeFontObject; // Character is a ScriptableObject and would render a unity object
                                                       // field if drawn in the inspector, which is not what we want.

            public ChangeFontObject mChangeAssetData
            {
                get { return this.changeFontObject; }
            }

            public ChangeFontWrapper(ChangeFontObject character)
            {
                this.changeFontObject = character;
            }

            [ReadOnly]
            [InfoBox("你可以单独点击某一个列表内的{改变字体}来调整这个列表内的字体，也可以点击{替换所有列表内的字体}")]
            [Button("替换所有列表内的字体", ButtonSizes.Large), GUIColor(0.2f, 0.8f, 0)]
            [PropertyOrder(1)]
            public void ChangeAllFont()
            {
                try
                {
                    foreach (var item in Go_Font)
                    {
                        item.ChangeFont();
                    }
                    Log.Debug("所有字体替换成功");

                }
                catch (System.Exception e)
                {

                    Log.Error("替换字体失败：{0}", e);
                }

            }


            [ShowInInspector]
            [PropertyOrder(2)]
            [TableList]
            [LabelText("所有需要改变的组")]
            public List<GoList> Go_Font { get { return this.changeFontObject.Go_Font; } set { this.changeFontObject.Go_Font = value; EditorUtility.SetDirty(this.changeFontObject); } }
        }
    }
}